<?php

namespace App\Models;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;

/**
 * Date: 2018/1/26
 * @author George
 * @method static select(array $columns = ['*'])
 * @package App\Models
 */
class Permission extends Model
{
    /**
     * 定义主键
     *
     * @var string
     * Date: 2018/1/25
     * @author George
     */
    protected $primaryKey = 'code';

    /**
     * 禁用主键自增
     *
     * @var bool
     * Date: 2018/1/25
     * @author George
     */
    public $incrementing = false;

    /**
     * 权限关联其他模型
     *
     * Date: 2018/1/26
     * @author George
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function permissiontable()
    {
        return $this->morphTo();
    }

    /**
     * 自定义隐式路由绑定实现缓存
     *
     * Date: 2018/1/25
     * @author George
     * @param mixed $value
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function resolveRouteBinding($value)
    {
        return Cache::remember("permissions:{$value}", 60, function () use ($value) {
            return $this->where($this->getRouteKeyName(), $value)->first();
        });
    }
}
